package com.zx.idc.backend.gui.slog.service;

import com.zx.idc.backend.gui.api.management.infosecurity.activeresources.service.ActiveResourceService;
import com.zx.idc.cache.entity.HotIpMetaInfo;
import com.zx.idc.cache.slog.SlogHotIpCacheService;
import com.zx.idc.common.configuration.AppProtocolConfiguration;
import com.zx.idc.common.lang.ListUtil;
import com.zx.idc.common.util.LocalDateTimeFormatUtil;
import com.zx.idc.ds.common.constant.ReportStatusConstant;
import com.zx.idc.ds.slog.entity.SlogHotIp;
import com.zx.idc.ds.slog.service.ISlogHotIpService;
import com.zx.idc.xml.entity.activeresources.ActiveResources;
import com.zx.idc.xml.entity.activeresources.findiplist.FindIpList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = false, rollbackFor = Exception.class)
public class SlogHotIpInterfaceService {

    @Autowired
    private ActiveResourceService activeResourceService;

    @Autowired
    private ISlogHotIpService iSlogHotIpService;

    @Autowired
    private SlogHotIpCacheService slogHotIpCacheService;

    /**
     * 上报未上报数据并更新上报状态
     *
     * @param slogHotIpList 未上报数据
     * @return 上报是否成功
     */
    public Boolean reportData(List<SlogHotIp> slogHotIpList) {
        //修改数据库状态
        if (slogHotIpList == null || slogHotIpList.size() == 0) {
            return true;
        }

        //组装数据
        ActiveResources activeResources = getActiveResources(slogHotIpList);

        //调用接口上报
        if (ListUtil.isNotEmpty(activeResources.getFindIpList())) {
            activeResourceService.reportData(activeResources);
        }
        return true;
    }

    private ActiveResources getActiveResources(List<SlogHotIp> entityList) {
        return new ActiveResources()
            .setFindIpList(
                entityList.stream()
                    .peek(uploadHotIp -> {
                        HotIpMetaInfo meta = slogHotIpCacheService.getMeta(Long.valueOf(uploadHotIp.getHouseId()), uploadHotIp.getFindIpListIp());
                        if (Objects.nonNull(meta)) {
                            Optional.ofNullable(meta.getVisitCnt()).ifPresent(uploadHotIp::setVisitsCount);
                            Optional.ofNullable(meta.getLstFoundTime()).ifPresent(lstFoundTime ->
                                uploadHotIp.setFindIpListLastTime(LocalDateTimeFormatUtil.formatLocalDateTime(lstFoundTime))
                            );
                        }
                    })
                    .map(obj ->
                        new FindIpList()
                            .setFindIpListIp(obj.getFindIpListIp())
                            .setFindIpListFirstTime(obj.getFindIpListFirstTime())
                            .setFindIpListLastTime(obj.getFindIpListLastTime())
                            .setBlock(obj.getBlock())
                            .setVisitsCount(obj.getVisitsCount())
                            .setIsInIpSeg(ReportStatusConstant.NOT_REPORTED)
                            .setPort(obj.getPort())
                            .setProtocol(obj.getProtocol())
                            .setIdcId(obj.getIdcId())
                            .setHouseId(obj.getHouseId())
                    ).collect(Collectors.toList())
            );
    }
}
